package com.twu.model;

import com.twu.biblioteca.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: FSW
 * Date: 25/05/13
 * Time: 下午 01:44
 * To change this template use File | Settings | File Templates.
 */
public class Library {
    private ArrayList<Customer> customers;
    private ArrayList<Book> books;
    private ArrayList<Movie> movies;
    private HashMap<Book, Customer> bookUserPair;
    private static int noOfCustomers = 0;

    public Library(){
        customers = new ArrayList<Customer>();
        books = new ArrayList<Book>();
        movies = new ArrayList<Movie>();
        bookUserPair = new HashMap<Book, Customer>();
        noOfCustomers = 0;
    }

    public Library(ArrayList<Customer> customersInput, ArrayList<Book> books) {
        noOfCustomers = 0;
        customers = new ArrayList<Customer>();
        for(Customer customer: customersInput){
            addCustomer(customer);

        }
        this.books = books;
        movies = new ArrayList<Movie>();
        bookUserPair = new HashMap<Book, Customer>();
    }

    public Library(ArrayList<Customer> customers, ArrayList<Book> books, ArrayList<Movie> movies) {
        this.customers = customers;
        this.books = books;
        this.movies = movies;
        bookUserPair = new HashMap<Book, Customer>();
    }

    public static int getNoOfCustomers() {
        return noOfCustomers;
    }

    public static void setNoOfCustomers(int noOfCustomers) {
        Library.noOfCustomers = noOfCustomers;
    }

    public HashMap<Book, Customer> getBookUserPair() {
        return bookUserPair;
    }

    public void setBookUserPair(HashMap<Book, Customer> bookUserPair) {
        this.bookUserPair = bookUserPair;
    }

    public ArrayList<Movie> getMovies() {
        return movies;
    }

    public void setMovies(ArrayList<Movie> movies) {
        this.movies = movies;
    }

    public ArrayList<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(ArrayList<Customer> customers) {
        this.customers = customers;
    }

    public ArrayList<Book> getBooks() {
        return books;
    }

    public Book getBookByIndex(int i){
        return books.get(i);
    }

    public Book getBookByIsbn(String isbn){
        for(Book book: books) {
            if(book.getIsbn().equals(isbn)) {
                return book;
            }
        }
        return null;
    }

    public void setBooks(ArrayList<Book> books) {
        this.books = books;
    }

    public void addCustomer(Customer customer){
        customer.setId(this);
        customers.add(customer);
        noOfCustomers++;
    }

    public void removeCustomer(Customer customer){
        customers.remove(customer);
    }

    public void addBook(Book book){
        books.add(book);
    }

    public void removeBook(Book book){
        books.remove(book);
    }

    public void addMovie(Movie movie){
        movies.add(movie);
    }

    public void removeMovie(Movie movie){
        movies.remove(movie);
    }

    public Customer getCustomerById(String id) {
        for(Customer customer: customers) {
            if(customer.getId().equals(id))
                return customer;
        }
        return null;
    }

    public void printBooks() {
        for(Book book:books){
             book.print();
        }
    }

    public void printMovies() {
        for(Movie movie:movies){
            movie.print();
        }
    }

    public boolean reserve(Book book, Customer user) {
        if(findHolder(book) != null)
            return false;
        else {
            bookUserPair.put(book, user);
            return true;
        }
    }

    public boolean cancelReserve(Book book, Customer user) {
        if(findHolder(book)!= user)
            return false;
        else{
            bookUserPair.remove(book);
            return true;
        }
    }

    public Customer findHolder(Book book){
        return bookUserPair.get(book);
    }

    public ArrayList<Book> getReserveBookList(Customer user){
        ArrayList<Book> bookList = new ArrayList<Book>();
        Iterator iterator = bookUserPair.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            if(entry.getValue().equals(user))
                bookList.add((Book) entry.getKey());
        }
        return bookList;

    }
}
